import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/report_obj.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/content_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_radio.dart';
import 'package:xws/widgets/app_text_field.dart';
import 'package:xws/widgets/safe_scaffold.dart';

class _Item {
  const _Item(this.value, this.content, this.type);

  final int value;
  final String content;
  final String type;
}

const List<_Item> _reportReason = <_Item>[
  _Item(0, '垃圾广告', 'AD'),
  _Item(1, '色情低俗', 'SEXY'),
  _Item(2, '攻击谩骂', 'VITUPERATION'),
  _Item(3, '政治敏感', 'POLITICS'),
  _Item(4, '血腥暴力', 'BLOODINESS'),
  _Item(5, '虚假描述', 'CHEAT'),
  _Item(6, '其他', 'OTHER'),
];

/// 举报类型
enum ReportType { USER, GOODS }

/// 举报页面
class ReportPage extends StatefulWidget {
  const ReportPage({
    Key? key,
    required this.reportType,
    required this.id,
  }) : super(key: key);

  final ReportType reportType;

  final String id;

  @override
  State<ReportPage> createState() => _ReportPageState();
}

class _ReportPageState extends State<ReportPage> with SafeState<ReportPage> {
  int? _select;

  bool get _isUser => widget.reportType == ReportType.USER;

  late final ReportObj _reportObj = ReportObj(
    targetId: widget.id,
    targetType: _isUser ? 'USER' : 'GOODS',
  );

  void _onTap(_Item v) {
    if (_select != v.value) {
      safeSetState(() => _select = v.value);
      _reportObj.reportType = v.type;
    }
  }

  /// 提交
  Future<void> _submit(BuildContext context) async {
    final String cr = _reportObj.check;

    if (cr != 'success') {
      ct(cr);
      return;
    }

    loading(context);
    final bool r = await reportContent(_reportObj);
    close(context);

    if (r) {
      st('举报成功', const Duration(seconds: 2));

      pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      titleText: widget.reportType == ReportType.GOODS ? '举报该物品' : '举报该用户',
      backgroundColor: white(context),
      actions: <Widget>[
        Padding(
          padding: const EdgeInsets.only(right: 10),
          child: AppBtn(onTap: () => _submit(context), text: '提交'),
        ),
      ],
      body: ListView(
        padding: const EdgeInsets.all(20),
        children: <Widget>[
          const Text(
            '举报理由:',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          ..._reportReason.map((_Item it) => _buildItem(it)).toList(),
          const SizedBox(height: 16),
          if (_select == 6) _description,
        ],
      ),
    );
  }

  /// 选项
  Widget _buildItem(_Item item) {
    return GestureDetector(
      onTap: () => _onTap(item),
      child: Container(
        color: Colors.transparent,
        padding: const EdgeInsets.symmetric(vertical: 8),
        child: Row(
          children: <Widget>[
            SimpleAppRadio(value: _select == item.value),
            const SizedBox(width: 10),
            Text(
              item.content,
              style: const TextStyle(fontSize: 14),
            ),
          ],
        ),
      ),
    );
  }

  Widget get _description {
    return AppTextField(
      hintText: '举报理由...',
      height: 160,
      maxLines: 200,
      maxLength: 10,
      initialValue: _reportObj.remark,
      onChanged: (String v) => _reportObj.remark = v,
      fillColor: canvas(context),
      border: OutlineInputBorder(
        borderSide: BorderSide.none,
        borderRadius: BorderRadius.circular(6),
      ),
    );
  }
}
